package com.hardydou.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hardydou.common.result.Result;
import com.hardydou.common.constant.Const.JobStatus;
import com.hardydou.common.controller.BaseController;
import com.hardydou.common.result.ResultHelper;
import com.hardydou.framework.log.anno.LogMetaInfo;
import com.hardydou.system.model.SystemJob;
import com.hardydou.system.service.ISystemJobService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * @author ：hardy
 * @description：岗位管理
 * @date ：Created in 2020-03-10
 * @Copyright ：Hardydou.com
 */
@Controller
@RequestMapping("/system/job")
@Api(tags = "岗位管理")
@LogMetaInfo(module = "岗位管理")
public class SysJobController extends BaseController {

    @Autowired
    ISystemJobService jobService;

    @ApiIgnore
    @GetMapping("")
    public String index() {
        return "job/list";
    }


    @ApiIgnore
    @GetMapping("/add")
    public String add() {
        return "job/add";
    }


    @ApiIgnore
    @PostMapping("/add")
    public String add(SystemJob job, Model model) {
        jobService.insert(job);
        model.addAttribute("code", 200);
        model.addAttribute("message", "添加成功");
        return "job/add";
    }

    @ApiIgnore
    @GetMapping("/edit")
    public String edit(long jobId, Model model) {
        SystemJob job = jobService.find(jobId);
        model.addAttribute("job", job);
        return "job/edit";
    }

    @ApiIgnore
    @PostMapping("/edit")
    public String edit(SystemJob job, Model model) {
        model.addAttribute("code", 200);
        model.addAttribute("message", "修改成功");
        jobService.update(job);
        model.addAttribute("job", job);
        return "job/edit";
    }


    @ApiIgnore
    @GetMapping("/info")
    public String info(long jobId, Model model) {
        SystemJob job = jobService.find(jobId);
        model.addAttribute("job", job);
        return "job/info";
    }


    /* JSON API */
    @GetMapping("/userJob.json")
    @ResponseBody
    public Result userJob(long userId) {
        List jobs = jobService.findAllByUserId(userId);
        return ResultHelper.ok(jobs);
    }

    @ApiOperation(value = "岗位列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", defaultValue = "10", required = true)
    })
    @GetMapping("/list.json")
    @ResponseBody
    public Result<?> listForJson(@RequestParam Map params) {
        PageHelper.startPage(getRequest());
        System.out.println(params);
        List<SystemJob> jobs = jobService.find(params);
        PageInfo pageInfo = new PageInfo(jobs);
        return ResultHelper.ok(pageInfo);
    }


    @ApiOperation(value = "添加岗位")
    @PostMapping("/add.json")
    @ResponseBody
    public Result<?> addForJson(SystemJob job) {
        jobService.insert(job);
        return ResultHelper.ok(job);
    }


    @ApiOperation(value = "删除岗位")
    @ApiImplicitParam(name = "jobId", value = "岗位ID", required = true)
    @DeleteMapping("/del")
    @ResponseBody
    public Result delete(long jobId) {
        jobService.delete(jobId);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("jobId", jobId);
        return result;
    }

    @ApiOperation(value = "更新岗位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "jobId", value = "岗位ID", required = true, paramType = "query")
    })
    @ApiParam(name = "jobId", value = "岗位ID", required = true)
    @PostMapping("/update")
    @ResponseBody
    public Result update(SystemJob job) {
        jobService.update(job);
        return ResultHelper.ok(job);
    }

    @ApiOperation(value = "更新状态")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "jobId", value = "岗位ID", required = true)
    )
    @PostMapping("/status")
    @ResponseBody
    public Result status(long jobId) {
        SystemJob job = jobService.find(jobId);
        job.setStatus(job.getStatus() == JobStatus.NORMAL.val ? JobStatus.LOCKED.val : JobStatus.NORMAL.val);
        jobService.update(job);
        Result<Map<String, Object>> result = ResultHelper.ok();
        result.getData().put("jobId", jobId);
        result.getData().put("status", job.getStatus());
        return result;
    }

}
